package org.vacoor.nothing.misc.express.usps.api.address;

import org.vacoor.nothing.misc.express.Uspsx;
import org.vacoor.nothing.misc.express.usps.api.UspsRequest;
import org.vacoor.nothing.misc.express.usps.api.data.UspsAddress;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 邮编查找
 * <p/>
 * ZIP Code Lookup Web Tool
 * https://www.usps.com/business/web-tools-apis/address-information-api.htm#_Toc365013009
 * 请求:
 * API: ZipCodeLookup
 * XML:
 * <pre>
 * <ZipCodeLookupRequest USERID="xxxxxxx">
 *      <Address ID="1">
 *          <Address1></Address1>
 *          <Address2>8 Wildwood Drive</Address2>
 *          <City>Old Lyme</City>
 *          <State>CT</State>
 *          <Zip5>06371</Zip5>
 *          <Zip4></Zip4>
 *      </Address>
 * </ZipCodeLookupRequest>
 * </pre>
 * 响应:
 * <pre>
 * <ZipCodeLookupResponse>
 *      <Address ID="0">
 *          <FirmName>XYZ CORP.</FirmName>
 *          <Address2>6406 IVY LN</Address2>
 *          <City>GREENBELT</City>
 *          <State>MD</State>
 *          <Zip5>20770</Zip5>
 *          <Zip4>1441</Zip4>
 *      </Address>
 *      <Address ID="1">
 *          <FirmName>ABC COMPANY</FirmName>
 *          <Address1>Apt/Suite 2</Address1>
 *          <Address2>435 S MAIN ST</Address2>
 *          <City>LOS ANGELES</City>
 *          <State>CA</State>
 *          <Zip5>90013</Zip5>
 *          <Zip4>1310</Zip4>
 *      </Address>
 *  </ZipCodeLookupResponse>
 * </pre>
 *
 * @author vacoor
 */
@XmlRootElement(name = "ZipCodeLookupRequest")
public class ZCLRequest extends UspsRequest {
    public static final String API = "ZipCodeLookup";

    @XmlElement(name = "Address")
    private List<UspsAddress> addresses;

    public ZCLRequest() {
        super(NO_REVISION);
        this.addresses = new ArrayList<UspsAddress>();
    }

    public ZCLRequest add(UspsAddress... addresses) {
        Collections.addAll(this.addresses, addresses);
        return this;
    }

    public static void main(String[] args) {
        Uspsx usps = Uspsx.create("034I2GLO7047", null);
        ZCLRequest req = new ZCLRequest();
        UspsAddress addr = new UspsAddress();
        addr.setFirmName("ABC COMPANY");
        addr.setAddress1("Apt/Suite 2");
        addr.setAddress2("435 S MAIN ST");
        addr.setCity("LOS ANGELES");
        addr.setZip5("90013");

        req.add(addr);
        String s = usps.callUspsApi(req.API, req);
        System.out.println(s);
    }
}
